'use strict';

// 登录 controller
app.controller('LoginCtrl', 
['$scope', '$rootScope', '$http', '$state','dialogs', '$timeout', 'LoginService',
function($scope, $rootScope, $http, $state, dialogs, $timeout, LoginService) {
	var vm = $scope.vm = this;
	
	vm.authError = false; 
	vm.user = {username:'', password:'', captcha:''};

	vm.enableCaptcha = false;
	$http.post(ISS.getPath('/enableCaptcha')).then(function(resp){
		vm.enableCaptcha = resp.data;
	});

	vm.getCode = function () {
		vm.codePath = ISS.getPath("/captchaCode?_dc="	+ (new Date()).getTime());
	}

	vm.getCode();
	
	vm.signup = function(){
		vm.authError = false;
		LoginService.login(vm.user).then(function(data){
			if(data.success){
				$scope.$emit('login-success');
				$state.go('app.form.basic');
			}else{
				vm.reason = data.reason;
				vm.authError = true;
			}
		});
	}
}])

